﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SPCarousel.SPCarousel
{
    public partial class SPCarouselUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            SPCarousel webpart = (SPCarousel)this.Parent;
            WriteButtonStyles(writer, webpart);
            //GenerateJavascriptFunctions(writer, webpart);
            WriteDimensionStyleSheet(writer, webpart);
            WriteProperties(writer, webpart);
            writer.Write("<ul id=\"" + webpart.ClientID + "_SPCarousel\" class=\"jcarousel-skin-tango\" style=\"height:"+(webpart.ItemHeight +60)+"px ></ul>");
            base.RenderControl(writer);
        }

        public void WriteProperties(HtmlTextWriter writer, SPCarousel webpart)
        {
            writer.Write("<div id=\"" + webpart.ClientID + "_SPCarouselProperties\">");
            writer.Write("<div id=\"SPCarouselRegister\" class=\"property\" style=\"display : none\">" + webpart.ClientID + "</div>");
            writer.Write("<div id=\"ListName\" class=\"property\" style=\"display : none\">" + webpart.Listname + "</div>");
            writer.Write("<div id=\"Auto\" class=\"property\" style=\"display : none\">" + webpart.AutoScroll + "</div>");
            writer.Write("<div id=\"Visible\" class=\"property\" style=\"display : none\">" + webpart.NumberOfIcons + "</div>");
            writer.Write("<div id=\"Verticle\" class=\"property\" style=\"display : none\">" + webpart.Vertical + "</div>");
            writer.Write("<div id=\"Arrows\" class=\"property\" style=\"display : none\">" + webpart.ShowArrows + "</div>");
            writer.Write("<div id=\"Wrap\" class=\"property\" style=\"display : none\">" + webpart.WrapCarousel + "</div>");
            writer.Write("<div id=\"margin\" class=\"property\" style=\"display : none\">" + webpart.PaddingX + "</div>");
            writer.Write("<div id=\"Width\" class=\"property\" style=\"display : none\">" + webpart.ItemWidth + "</div>");
            writer.Write("<div id=\"Height\" class=\"property\" style=\"display : none\">" + webpart.ItemHeight + "</div>");
            writer.Write("<div id=\"TextLinks\" class=\"property\" style=\"display : none\">" + webpart.Textlinks + "</div>");
            writer.Write("</div>");
        }

        //public void GenerateJavascriptFunctions(HtmlTextWriter writer, SPCarousel webpart)
        //{
        //    string functionHeader = "function ";
        //    string getDataFunctionName = webpart.ClientID + "GetData";
        //    string createAssociativeArrayFunctionName = webpart.ClientID + "CreateAssociativeArray";
        //    string generateListItemName = webpart.ClientID + "GenerateListItem";
        //    string getDataFunction;
        //    string createAssociativeArrayFunction;
        //    string generateListItem;
        //    string documentreadyFunction;

        //    getDataFunction = functionHeader + getDataFunctionName + "() {\n\n";
        //    getDataFunction += "var Data = new Array();\n\n";
        //    getDataFunction += webpart.GetData;
        //    getDataFunction +="return Data;\n}" ;

        //    createAssociativeArrayFunction = functionHeader + createAssociativeArrayFunctionName + "(dataObject) {\n\n";
        //    createAssociativeArrayFunction += "var returnArray = new Array();\n";
        //    createAssociativeArrayFunction += webpart.CreateAssociativeArray;
        //    createAssociativeArrayFunction += " return returnArray;\n}\n\n";

        //    generateListItem = functionHeader + generateListItemName + "(associativeArray) {\n\n";
        //    generateListItem += webpart.GenerateListItem;
        //    generateListItem += "}\n";

        //    documentreadyFunction = "$(document).ready(function () {\ninitilizeProperties();\n";
        //    documentreadyFunction += "var data = " + getDataFunctionName + "()\n\n";
        //    documentreadyFunction += "for (var i = 0; i < data.length; i++) {\n\n";
        //    documentreadyFunction += " var associativeArray = " + createAssociativeArrayFunctionName + "(data[i]);\n\n";
        //    documentreadyFunction += " var generatedListItem = " + generateListItemName + "(associativeArray);\n\n";
        //    documentreadyFunction += "$(\"#mycarousel\").append(\"<li>\" + generatedListItem + \"</li>\");\n}\n";
        //    documentreadyFunction += "$('#mycarousel').jcarousel({visible: _Wvisible,scroll: 1,wrap: _circular,auto: _Wauto,vertical: _Wverticle,buttonNextHTML: _arrows,buttonPrevHTML: _arrows});\n";

        //    writer.Write("<script type=\"text\\javascript\">");
        //    writer.Write(getDataFunction);
        //    writer.Write(createAssociativeArrayFunction);
        //    writer.Write(generateListItem);
        //    writer.Write(documentreadyFunction);
        //    writer.Write("</script>");


        //}

        public void WriteDimensionStyleSheet(HtmlTextWriter writer, SPCarousel webpart)
        {
            int totalWidth;
            int totalHeight;
            int itemWidth = webpart.ItemWidth;
            int itemHeight = webpart.ItemHeight;
            int carouselPaddingX = webpart.PaddingX;
            //Set Generic Data
            writer.Write("<style type=\"text/css\">");
            writer.Write(".jcarousel-skin-tango .jcarousel-item {width:" + itemWidth + "px; height:" + itemHeight + "px;}");
            //Set horizontal data
            totalWidth = (itemWidth+carouselPaddingX) * webpart.NumberOfIcons;
            totalHeight = itemHeight;
            writer.Write(".jcarousel-skin-tango .jcarousel-container-horizontal{padding: 20px 40px; width:" + totalWidth + "px;height:" + totalHeight + "px;}");
            writer.Write(".jcarousel-skin-tango .jcarousel-clip-horizontal{ width:" + totalWidth + "px;height:" + totalHeight + ";}");
            //Set vertical data
            totalWidth = itemWidth;
            totalHeight = itemHeight * webpart.NumberOfIcons;
            writer.Write(".jcarousel-skin-tango .jcarousel-container-vertical{padding: 20px 40px; width:" + totalWidth + "px;height:" + totalHeight + "px;}");
            writer.Write(".jcarousel-skin-tango .jcarousel-clip-vertical{ width:" + totalWidth + "px;height:" + totalHeight + "px}");
            writer.Write("</style>");
        }

        public void WriteButtonStyles(HtmlTextWriter writer, SPCarousel webpart)
        {
            string leftarrow = webpart.LeftArrowURL;
            string rightarrow = webpart.RightArrowURL;
            string leftarrowVertical = webpart.LeftArrowVerticalURL;
            string rightarrowVertical = webpart.RightArrowVerticalURL;
            writer.Write("<style type=\"text/css\">" + webpart.Stylesheet + "</style>");
            string horizontalButtons = @".jcarousel-skin-tango .jcarousel-prev-horizontal {
            background: transparent url(" + rightarrow + @") no-repeat 0 0;
            }

            .jcarousel-skin-tango .jcarousel-direction-rtl .jcarousel-prev-horizontal {
                background-image: url(" + rightarrow + @");
            }
            .jcarousel-skin-tango .jcarousel-next-horizontal {
                  background: transparent url(" + leftarrow + @") no-repeat 0 0;
            }

            .jcarousel-skin-tango .jcarousel-direction-rtl .jcarousel-next-horizontal {
                background-image: url(" + leftarrow + @");
            }";

            string veritcalButtons = @".jcarousel-skin-tango .jcarousel-prev-vertical {
            background: transparent url(" + rightarrow + @") no-repeat 0 0;
            }

            .jcarousel-skin-tango .jcarousel-direction-rtl .jcarousel-prev-vertical {
                background-image: url(" + rightarrow + @");
            }
            .jcarousel-skin-tango .jcarousel-next-vertical {
                  background: transparent url(" + leftarrow + @") no-repeat 0 0;
            }

            .jcarousel-skin-tango .jcarousel-direction-rtl .jcarousel-next-vertical {
                background-image: url(" + leftarrow + @");
            }";
            writer.Write("<style type=\"text/css\">" + horizontalButtons + "</style>");
            writer.Write("<style type=\"text/css\">" + veritcalButtons + "</style>");
        }
    }
}
